# -*- coding: utf-8 -*-
'''

程序异常处理接口
/*PHP是世界上最好的语言*/

'''




import json,requests,time, os
from base_config import *
from random import choice
from manage.helper import *
from requests.exceptions import ConnectionError

class Exceptions( object ):

    def __init__(self):

        pass

    # 记录错误日志
    @staticmethod
    def errorWriteLog(data ,filename = '网络异常错误日志'):

        cwd = os.getcwd()

        save_dir = cwd + '\\' + '{0}'.format(Helper.getCurrentDate())

        if not os.path.exists(save_dir): \

                os.mkdir(save_dir)

        with open(save_dir + '\\{0}.txt'.format( filename ), mode='a') as f:

            content = json.dumps(data, ensure_ascii=False)

            f.write(content + '\n')

        f.close()

    '''
    任务失败处理
    '''

    @staticmethod
    def failTask( data  ):


        r = requests.post( url = domain_url['domain']['test_fail_url'], data=data )

        code = r.json()['code']

        # 任务完成
        if r.status_code == 200 and code == 0:

            pass


        # 签名错误 返回任务失败 失败[101]
        elif r.status_code == 200 and code == 101:

            data['error'] = '签名错误'

            Exceptions.errorWriteLog(json.dumps(data), filename='失败接口错误日志')

        # 服务器错误 返回任务失败 失败[102]
        elif r.status_code == 200 and code == 102:

            data['error'] = '服务器错误'

            Exceptions.errorWriteLog(json.dumps(data), filename='失败接口错误日志')

        # 参数不合法错误 返回任务失败 失败[103]
        elif r.status_code == 200 and code == 103:

            data['error'] = '参数不合法'

            Exceptions.errorWriteLog(json.dumps(data), filename='失败接口错误日志')

        else:

            data['error'] = '未知错误'

            Exceptions.errorWriteLog(json.dumps(data), filename='失败接口错误日志')

    '''
    任务成功处理
    '''
    @staticmethod
    def successTask( data ):

        r = requests.post(url=domain_url['domain']['test_success_url'], data=data)

        code = r.json()['code']
        # 任务完成
        if r.status_code == 200 and code == 0:

            pass

        # 签名错误 返回任务成功 失败[101]
        elif r.status_code == 200 and code == 101:

            data['error'] = '签名错误'

            Exceptions.errorWriteLog( json.dumps( data),filename='成功接口错误日志')

        # 服务器错误 返回任务成功 失败[102]
        elif r.status_code == 200 and code == 102:

            data['error'] = '服务器错误'

            Exceptions.errorWriteLog(json.dumps(data), filename='成功接口错误日志')

        # 参数不合法错误 返回任务成功 失败[103]
        elif r.status_code == 200 and code == 103:

            data['error'] = '参数不合法'

            Exceptions.errorWriteLog(json.dumps(data), filename='成功接口错误日志')

        else:

            data['error'] = '未知错误'

            Exceptions.errorWriteLog(json.dumps(data), filename='成功接口错误日志')


    '''
    网络重启
    '''
    @staticmethod
    def resetConnet( ):

        import os

        # 网络断开
        disableConnet = 'Rasdial "拨号连接" /disconnect'

        os.system( disableConnet )


        # 循环请求百度 查看网络是否连接正常
        while True:

            try:
                url = choice(urls)

                r = lambda : requests.get(url = url )

                time.sleep(1)

                if r().status_code == 200:

                    ip = lambda: requests.get(url='http://ip.42.pl/raw').text


                    print('当前ip地址为:', ip())

                    break

            except ConnectionError:

                print('*----------------正在进行IP更换请稍等---------------------*')

            time.sleep(2)



if __name__ == '__main__':



    Exceptions.resetConnet()












